<?php

/**
 * 投票
 * 
 * @filepath Apps/Wechat/Controller/VoteController.class.php
 * @author Zix <zix2002@gmail.com>
 * @version  1.0 , 2015-09-13
 */

namespace Wechat\Controller;

use Common\Service\WxEventVoteService;
use Common\Service\AttachmentsService;

class VoteController extends WechatController {

  public function index() {

    //检查参数是否接收成功
    if (empty($this->mer_id) || empty($this->wx_id)) {
      gameover('param error'); //未取到微信信息
    }

    //检查事件ID
    $event_id = I('get.event_id', '');
    if (empty($event_id)) {
      gameover('unkown event');
    }
    
    //取活动信息
    $WxEventVote = WxEventVoteService::instance();
    $data['event_data'] = $WxEventVote->get_by_id($event_id);
    
    if( empty( $data['event_data'] ) ) {
      gameover('活动未找到');
    }
    
    //取用户信息
    $userinfo = $this->_get_user_info();    
    $data['user_info'] = $userinfo;
    
    //print_r($userinfo);
    //取参数字符串
    $url_query_string = '?' .getenv('QUERY_STRING');
    
    //准备分享数据
    $share_url = U('Wechat/Vote/index', '', TRUE, TRUE) . $url_query_string ;
    $share_data = array(
      'title' => $data['event_data']['share_title'],
      'desc'  => $data['event_data']['share_desc'],
      'icon'  => U( $data['event_data']['icon'] , '', TRUE, TRUE),
      'link'  => $this->WechatSdk->getOauthRedirect($share_url, '' , 'snsapi_userinfo'),
    );
    
    //js sdk 参数
    $data['js_config'] = $this->_get_js_config();

    //各种url
    $data['config'] = array(
      'url_query_string' => $url_query_string ,
      'player_list_url' => U('Wechat/Vote/player_list') . "/$event_id",
      'signin_url' => U('Wechat/Vote/signin') . $url_query_string ,
      'do_vote_url' => U('Wechat/Vote/do_vote') . $url_query_string ,
      'share_data' => $share_data 
    );
    
    $this->assign($data);
    $this->display();
  }

  /**
   * 投票列表
   */
  function player_list( $event_id ) {
    $WxEventVotePlayer = \Common\Service\WxEventVotePlayerService::instance();
    
    $config = array(
      'keyword' => I('post.keyword'),
      'event_id' => $event_id ,
      'status' => 1 ,
      'page' => I('post.page') ,
      'page_size' => 20 ,
      'sort' => I('post.sort') ,
      'order' => 'DESC' , 
    );
    
    $data['detail_url'] = U('Wechat/Vote/detail') . '/';
    $data['url_query_string'] = I('post.url_query_string');
    $data['player_list'] = $WxEventVotePlayer->get_by_cond($config);
    
    $config['count'] = TRUE ;
    $ret['total'] = $WxEventVotePlayer->get_by_cond($config);
    
    $this->assign($data);
    $ret['html'] = $this->fetch();

    $this->ajaxReturn($ret);
  }

  /**
   * 投票
   */
  function do_vote() {
    $event_id = I('get.event_id');
    $player_id = I('post.id');
    
    //取用户信息
    $userinfo = $this->_get_user_info();
    //$userinfo['user_id'] = $userinfo['user_id'] ; //测试
    
    //投票
    $WxEventVote = WxEventVoteService::instance();
    $ret = $WxEventVote->do_vote($event_id , $userinfo['user_id'], $player_id);
    $this->ajaxReturn($ret);
  }

  /**
   * 参数权手详情
   */
  function detail( $player_id ) {
    //检查参数是否接收成功
    if (empty($this->mer_id) || empty($this->wx_id)) {
      die('param error'); //未取到微信信息
    }

    //检查事件ID
    $event_id = I('get.event_id', '');
    if (empty($event_id)) {
      die('unkown event');
    }

    //取用户信息
    $userinfo = $this->_get_user_info();
    $data['user_info'] = $userinfo;
    
    //取参数字符串
    $url_query_string = '?' .getenv('QUERY_STRING');
    
    //取活动信息
    $WxEventVote = WxEventVoteService::instance();
    $data['event_data'] = $WxEventVote->get_by_id($event_id);

    //去player 信息
    $WxEventVotePlayer = \Common\Service\WxEventVotePlayerService::instance();
    $data['player_data'] = $WxEventVotePlayer->get_by_id( $player_id );
    
    //取图片附件
    $Attachments = AttachmentsService::instance();
    $data['attach_data'] = $Attachments->get_by_cond(array(
      'owner' => AttachmentsService::WX_EVENT_VOTE ,
      'owner_id' => $player_id ,
    ));

    //准备分享数据
    $share_url = U('Wechat/Vote/detail', '', TRUE, TRUE) . $url_query_string ;
    $share_data = array(
      'title' => $data['event_data']['share_title'],
      'desc'  => $data['event_data']['share_desc'],
      'icon'  => U( $data['event_data']['icon'] , '', TRUE, TRUE),
      'link'  => $this->WechatSdk->getOauthRedirect($share_url, '' , 'snsapi_userinfo'),
    );
    
    //js sdk 参数
    $data['js_config'] = $this->_get_js_config();

    //各种url
    $data['config'] = array(
      'url_query_string' => $url_query_string ,
      'signin_url' => U('Wechat/Vote/signin') . $url_query_string ,
      'do_vote_url' => U('Wechat/Vote/do_vote') . $url_query_string ,
      'share_data' => $share_data 
    );
    
    $this->assign($data);
    $this->display(); 
  }
  
  /**
   * 
   */
  function signin() {
  //检查参数是否接收成功
    if (empty($this->mer_id) || empty($this->wx_id)) {
      die('param error'); //未取到微信信息
    }

    //检查事件ID
    $event_id = I('get.event_id', '');
    if (empty($event_id)) {
      die('unkown event');
    }

    //取用户信息
    $userinfo = $this->_get_user_info();
    //print_r(session( self::SESSION_NAME ) );
    //$data['']['user_info'] = $userinfo;
    
    //print_r( $userinfo );
    //取参数字符串
    $url_query_string = '?' .getenv('QUERY_STRING');
    
    //取活动信息
    $WxEventVote = WxEventVoteService::instance();
    $data['event_data'] = $WxEventVote->get_by_id($event_id);

    //js sdk 参数
    $data['js_config'] = $this->_get_js_config();

    //各种url
    $data['config'] = array(
      'user_info' => $userinfo ,
      'url_query_string' => $url_query_string ,
      'signin_url' => U('Wechat/Vote/signin') . $url_query_string ,
      'do_signin_url' => U('Wechat/Vote/do_signin') . "/$event_id" . $url_query_string ,
      'upload_icon_url' => U('Wechat/Vote/upload') .'/icon',
      'del_icon_url' => U($this->base_path . 'delete_icon') ,
      'detail_url' => U('Wechat/Vote/detail') . '/' ,
    );
    
    $this->assign($data);
    $this->display();  
  }

  /**
   * 参加比赛
   */
  function do_signin( $event_id ) {
    $data = I('post.');
    $attach = $data['attach'];
    unset( $data['attach'] );
    
    $data['icon'] = $attach[0]['savePath'];
    $data['event_id'] = $event_id ;
    //取用户信息
    $userinfo = session( self::SESSION_NAME ); 
    
    if( empty( $userinfo ) ) {
      $this->ajaxReturn(ajax_arr('用户信息错误'));
    }
    
    $data['user_id'] = $userinfo['user_id'] ;
    //添加参赛者
    $WxEventVotePlayer = \Common\Service\WxEventVotePlayerService::instance();
    $ret = $WxEventVotePlayer->create($data);
    
    if ( !$ret['status'] ) {
      $this->ajaxReturn( $ret );
    }
    
    if( count( $attach) > 1 ) {
      //添加附件
      $Attachments = AttachmentsService::instance();
      $attatch_data = array();
      for( $i = 1; $i < count( $attach) ; $i++ ) {
        $attatch_data[] = array(
          'owner' => AttachmentsService::WX_EVENT_VOTE ,
          'owner_id' => $ret['id'] ,
          'filename' => $attach[$i]['name'] ,
          'savepath' => $attach[$i]['savePath'] ,
          'size' => $attach[$i]['size'] ,
          'mimes' => $attach[$i]['type'] ,
        );
      }
      $Attachments->create_all($attatch_data );
    }
    
    $this->ajaxReturn(ajax_arr('报名成功，请等待审核' , TRUE , array(
      'id' => $ret['id']
    )));
  }


}
